﻿HmJS.$Import('core.core');

/*
---
script: MooSocket.js
version: 0.3.1-0
description: MooSocket is a basic wrapper for WebSockets in MooTools, this should hopefully make working with the WebSocket API easier and more familiar to MooTools Devlopers.
license: MIT-style
download: http://mootools.net/forge/p/moosocket
source: http://github.com/trobrock/moosocket

authors:
- Trae Robrock

requires:
- core/1.3: '*'

provides: [MooSocket]
...
*/

HmJS.register('util.MooSocket', function ($ns) {

	return new Class({

		Implements: [Options, Events],

		options: {
			reconnect: true,
			maxReconnects: 10,
			onOpen: Function.from(),
			onMessage: Function.from(),
			onClose: Function.from(),
			onError: Function.from()
		},
		reconnectDelay: 0,
		reconnectAttempts: 0,

		initialize: function (location, options) {
			if (!("WebSocket" in window)) { return false; }

			this.setOptions(options);
			this.location = location;

			this.create();
		},

		create: function () {
			this.socket = new WebSocket(location);
			this.attachEvents();
		},

		attachEvents: function () {
			this.socket.onmessage = function (e) {
				this.fireEvent("message", [e.data, e]);
			} .bind(this);

			this.socket.onclose = function (e) {
				this.fireEvent("close");
				if (this.options.reconnect) this.reconnect();
			} .bind(this);

			this.socket.onopen = function (e) {
				this.fireEvent("open", [e.data, e]);
			} .bind(this);

			this.socket.onerror = function (e) {
				this.fireEvent("error", [e.data, e]);
			} .bind(this);

			return this;
		},

		reconnect: function () {
			if (this.reconnectAttempts > this.options.maxReconnects) { return false; }
			this.create.delay(this.reconnectDelay * 1000);
			this.reconnectAttempts++;
			this.reconnectDelay = 2 * this.reconnectAttempts;
		}
	});

});
